package com.hsgene.user.support.dto;

import com.hsgene.common.util.exception.ErrorCode;
import com.hsgene.common.util.redis.RedisConstants;

import java.io.Serializable;

/**
 * 获取微信用户信息
 *
 * @author wangbing
 * @version 1.0, 2018/10/16
 */
@SuppressWarnings("all")
public class WeChatSessionResponse implements Serializable {

    private static final long serialVersionUID = 720772600426501338L;
    private static final int SUCCESS_CODE = 0;

    /**
     * Redis缓存键值
     */
    public static final String WECHAT_SESSION_RESPONSE_CACHE_KEY = RedisConstants.DEFAULT_REDIS_PREFIX + RedisConstants.COLON_SPLIT + "wechat_session_response:";

    /**
     * openid
     */
    private String openid;

    /**
     * unionid
     */
    private String unionid;

    /**
     * session_key 解密秘钥
     */
    private String session_key;

    /**
     * 错误码
     */
    private int errcode = SUCCESS_CODE;

    /**
     * 错误信息
     */
    private String errmsg;

    public boolean isSuccess() {
        return SUCCESS_CODE == errcode;
    }

    public ErrorCode getErrorCode() {
        if (isSuccess()) {
            return null;
        }

        return new ResponseErrorCode(this);
    }

    public String getOpenid() {
        return openid;
    }

    public void setOpenid(String openid) {
        this.openid = openid;
    }

    public String getUnionid() {
        return unionid;
    }

    public void setUnionid(String unionid) {
        this.unionid = unionid;
    }

    public String getSession_key() {
        return session_key;
    }

    public void setSession_key(String session_key) {
        this.session_key = session_key;
    }

    public int getErrcode() {
        return errcode;
    }

    public void setErrcode(int errcode) {
        this.errcode = errcode;
    }

    public String getErrmsg() {
        return errmsg;
    }

    public void setErrmsg(String errmsg) {
        this.errmsg = errmsg;
    }

    class ResponseErrorCode implements ErrorCode {
        private int code;

        private String message;

        private String description;

        public ResponseErrorCode() {
        }

        public ResponseErrorCode(WeChatSessionResponse response) {
            this(response.getErrcode(), response.getErrmsg(), response.getErrmsg());
        }

        public ResponseErrorCode(int code, String message, String description) {
            this.code = code;
            this.message = message;
            this.description = description;
        }

        @Override
        public int getCode() {
            return code;
        }

        @Override
        public String getMessage() {
            return message;
        }

        @Override
        public String getDescription() {
            return description;
        }
    }
}
